package com.yunxin.utils.db;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBUtil {


    public static void initDatabaseDriver(String driver) throws ClassNotFoundException {
        Class.forName(driver);
    }

    public static  String DRIVER_ORACLE = "oracle.jdbc.driver.OracleDriver";
    public static  String DRIVER_MYSQL8 = "com.mysql.cj.jdbc.Driver";
    public static  String DRIVER_MYSQL5 = "com.mysql.jdbc.Driver";


    public static List<Map> select(Connection connection, String sql) throws SQLException {
        Statement selectStmt = connection.createStatement();
        ResultSet resultSet = selectStmt.executeQuery(sql);
        /**
         * 获取列名
         */
        ResultSetMetaData rsmd = resultSet.getMetaData();
        int count=rsmd.getColumnCount();
        String[] cols=new String[count];
        for(int i=0;i<count;i++){
            cols[i]=rsmd.getColumnName(i+1);}

        /**
         * 读取数据
         */
        List<Map> list = new ArrayList<>();
        while (resultSet.next()){
            Map map = new HashMap();
            for(String col: cols){
                map.put(col,resultSet.getObject(col));
            }
            list.add(map);
        }
        return list;
    }


    public static void update(Connection connection, String sql) throws  SQLException{
        Statement selectStmt = null;
        try{
            connection.setAutoCommit(false);
            selectStmt = connection.prepareStatement(sql);
            selectStmt.executeUpdate(sql);
            connection.commit();
        }catch (Throwable t){
            connection.rollback();
        }
    }

    public static Connection buildConnection(String url, String user, String password) throws  SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }


    public static Short getShort(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj).shortValue();
        }else if(obj instanceof String){
            return Short.parseShort((String) obj);
        }else{
            throw new RuntimeException("unknownType");
        }
    }

    public static Timestamp geTimestamp(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof Timestamp){
            return (Timestamp) obj;
        }else{
            throw new RuntimeException("unknow type: "+obj+" type:"+obj.getClass());
        }
    }

    public static BigDecimal getBigDecimal(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj);
        }else if(obj instanceof String){
            return new BigDecimal((String) obj);
        }else{
            throw new RuntimeException("unknownType");
        }
    }

    public static Long getLong(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj).longValue();
        }else if(obj instanceof String){
            return Long.parseLong((String) obj);
        }else if(obj instanceof Long){
            return (Long) obj;
        }
        else{
            throw new RuntimeException("unknownType");
        }
    }

    public static Integer getInt(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj).intValue();
        }else if(obj instanceof String){
            return Integer.parseInt((String) obj);
        }else if(obj instanceof Integer){
            return (Integer) obj;
        }else{
            throw new RuntimeException("unknownType");
        }
    }

    public static Double getDouble(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        if(obj instanceof BigDecimal){
            return ((BigDecimal) obj).doubleValue();
        }else if(obj instanceof Double){
            return (double) obj;
        }else if(obj instanceof String){
            return Double.parseDouble((String) obj);
        }else{
            throw new RuntimeException("unknownType");
        }
    }

    public static String getString(String name, Map map){
        Object obj = map.get(name);
        if(obj==null){
            return null;
        }
        return obj.toString();
    }


}
